#include "includes.h"

volatile unsigned char counter_pwm;

/*****************************************************************************
Name:           Delay
Description:    Delay program for ...ms
Parameters:     ms
Returns:        none
*****************************************************************************/

void Delay(int ms)
{
  int i;

  for (i=0;i<ms;i++)
      _delay_ms(1);
}

/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/

void buzzer_init(void)
{
	DDRB|=(1<<4);
}


/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/
void buzzer(unsigned int repeat)
{
    unsigned char i=0;
	while(i<repeat)
	{
	BUZZER_ON;
	_delay_ms(1);
	BUZZER_OFF;
	_delay_ms(1);
	i++;
	}
}



/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/

void temp2lcd(void)
{

	lcd_gotoxy(0,0);
	adc_init(0x00);
    adc_lcd();  
	lcd_puts(" ");
	read_temperature();	
	Delay(1000);
}

void counter2lcd(void)
{
	char buffer[4];
	itoa(counter_pwm,buffer,10);
	lcd_gotoxy(0,1);
	lcd_puts(buffer);
}

/*****************************************************************************
Name:           
Description:    
Parameters:     
Returns:        
*****************************************************************************/
ISR (INT2_vect) 
{
    SREG = 0;
	Delay(300);
	lcd_clrscr();
	
	/*
	if(counter_pwm==0) pwm_init();
	if(counter_pwm<245)
	{
		counter_pwm+=10;
		counter2lcd();
    	pwm_setduty(counter_pwm);
		
	}*/
	read_temperature();
    SREG |= 0x80;
}
ISR (INT1_vect) 
{
    SREG = 0;
	Delay(300);
	lcd_clrscr();
	
	/*
	if(counter_pwm>10)
	{
		counter_pwm-=10;
		counter2lcd();
		pwm_setduty(counter_pwm);
		
	}*/
	adc_lcd();
	
    SREG |= 0x80;
}
ISR (INT0_vect) 
{
    SREG = 0;
	counter_pwm=0;
    counter2lcd();
    pwm_setduty(0);
	pwm_off();
	BUZZER_OFF;
    Delay(300);
    SREG |= 0x80;
}

void interrupt_init(void){
  
    GICR = 0;
    GICR &= 0xDF;
    GIFR |= (1<<INTF2)|(1<<INTF1)|(1<<INTF0);
    GICR |= (1<<INT2)|(1<<INT1)|(1<<INT0);
    SREG |= 0x80;

}